package com.zzptc.gyc.myapp11;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.util.Log;

import com.zzptc.gyc.myapp11.Service.MyAppService;
import com.zzptc.gyc.myapp11.adapter.MyRvAdapter;
import com.zzptc.gyc.myapp11.entity.RecipeModel;
import com.zzptc.gyc.myapp11.entity.ResultsBean;
import com.zzptc.lzh.myapp11.R;
import com.zzptc.lzh.myapp11.databinding.ActivityMainBinding;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("https://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    private static final String TAG = "MainActivity";
    ActivityMainBinding binding;//ActivityMainBinding是系统生成的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding =DataBindingUtil.setContentView(this, R.layout.activity_main);//加载布局,用dataBinding来实例化
        //RecyclerView rv_id=findViewById(R.id.rv_id);
        //bingding.rv
        getAllRecipe();
    }

    private void getAllRecipe() {
        MyAppService myAppService = retrofit.create(MyAppService.class);
        Call<RecipeModel> all = myAppService.getAll(5);
        all.enqueue(new Callback<RecipeModel>() {
            @Override
            public void onResponse(Call<RecipeModel> call, Response<RecipeModel> response) {
                List<ResultsBean> results = response.body().getResults();
                for (ResultsBean r:
                        results) {
                    r.save();
                }
                MyRvAdapter adapter=new MyRvAdapter(MainActivity.this);
                binding.rv.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<RecipeModel> call, Throwable t) {
                Log.e(TAG, "onFailure: ",t );

            }
        });
    }
}
